package model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户实体类
 */
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    private String username;    // 用户名
    private String password;    // 密码
    private String role;        // 角色：admin/user
    private boolean loggedIn;   // 登录状态
    private List<String> orderHistory; // 订单历史
    private long registrationTime; // 注册时间

    public User(String username, String password, String role) {
        this.username = username;
        this.password = password;
        this.role = role;
        this.loggedIn = false;
        this.orderHistory = new ArrayList<>();
        this.registrationTime = System.currentTimeMillis();
    }

    // Getters and Setters
    public String getUsername() { return username; }
    public void setUsername(String username) { this.username = username; }

    public String getPassword() { return password; }
    public void setPassword(String password) { this.password = password; }

    public String getRole() { return role; }
    public void setRole(String role) { this.role = role; }

    public boolean isLoggedIn() { return loggedIn; }
    public void setLoggedIn(boolean loggedIn) { this.loggedIn = loggedIn; }

    public long getRegistrationTime() {
        return registrationTime;
    }
    /**
     * 检查用户是否为管理员
     */
    public boolean isAdmin() {
        return "admin".equalsIgnoreCase(role);
    }

    @Override
    public String toString() {
        return String.format("用户: %s, 角色: %s, 状态: %s",
                username, role, loggedIn ? "已登录" : "未登录",registrationTime, registrationTime);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return username.equals(user.username);
    }

    @Override
    public int hashCode() {
        return username.hashCode();
    }
}